package com.jdcy.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jdcy.model.service.BaseService;
import com.jdcy.utils.ResultJson;

import io.swagger.annotations.ApiOperation;

@RestController
public abstract class BaseController<T> {

	@Autowired
	private BaseService<T> baseService;

	@ApiOperation(value = "添加")
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResultJson add(@RequestBody T t) {
		ResultJson json = new ResultJson();
		Integer row = baseService.add(t);
		if (row > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
		}
		return json;
	}

	@ApiOperation(value = "修改")
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public ResultJson update(@RequestBody T t) {
		ResultJson json = new ResultJson();
		Integer row = baseService.update(t);
		if (row > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
		}
		return json;
	}

	@ApiOperation(value = "刪除")
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public ResultJson delele(@PathVariable Integer id) {
		ResultJson json = new ResultJson();
		Integer row = baseService.delete(id);
		if (row > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
		}
		return json;
	}

	@ApiOperation(value = "ID 查")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public ResultJson queryById(@PathVariable Integer id) {
		ResultJson json = new ResultJson();
		T T = baseService.queryById(id);
		if (T != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(T);
		}
		return json;
	}

	@ApiOperation(value = "查所有")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResultJson query() {
		ResultJson json = new ResultJson();
		List<T> tlist = baseService.query();
		if (tlist != null && tlist.size() > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(tlist);
		}
		return json;
	}

}
